package com.gagakuai.bpm.config.mybatisplus.config;


/*
 * @program: bpm_server
 * @description:
 * @author: houhong
 * @create: 2022-05-10 11:32
 */
//@Configuration
//@MapperScan(basePackages = "com.gagakuai.bpm.dao.mapper")
public class MybatisConfig {
/*
    @Bean
    public PaginationInterceptor paginationInnerInterceptor() {
        return new PaginationInterceptor();
    }

    @Bean(name = "complex")
    @ConfigurationProperties(prefix = "spring.datasource.druid.complex")
    public DataSource complex() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "complexBus")
    @ConfigurationProperties(prefix = "spring.datasource.druid.complex-bus")
    public DataSource complexBus() {
        return DruidDataSourceBuilder.create().build();
    }


    @Bean
    @Primary
    public DataSource multipleDataSource(@Qualifier("complex") DataSource complex, @Qualifier("complexBus") DataSource complexBus) {
        MultipleDataSource multipleDataSource = new MultipleDataSource();
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceEnum.Complex.getValue(), complex);
        targetDataSources.put(DataSourceEnum.ComplexBus.getValue(), complexBus);
        //添加数据源
        multipleDataSource.setTargetDataSources(targetDataSources);
        //设置默认数据源
        multipleDataSource.setDefaultTargetDataSource(complexBus);
        return multipleDataSource;
    }


    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(multipleDataSource(complex(), complexBus()));
        //这里注意 如果你用了mybatis的xml的文件查询，路径要改*/
       // sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/*/*Mapper.xml"));

      /*  MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setCacheEnabled(false);
        sqlSessionFactory.setConfiguration(configuration);
        sqlSessionFactory.setPlugins(new Interceptor() {
            @Override
            public Object intercept(Invocation invocation) {
                return paginationInnerInterceptor();
            }
        });
        return sqlSessionFactory.getObject();
    }

    // 配置事物管理器
    @Bean(name = "transactionManager")
    public DataSourceTransactionManager transactionManager() {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(complex());
        transactionManager.setDataSource(complexBus());
        return transactionManager;
    }
    */
    /*
     * SQL执行效率插件
     */
    /*@Bean
    @Profile({"dev","test"})// 设置 dev test 环境开启
    public PerformanceInterce performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        performanceInterceptor.setMaxTime(1000);
        performanceInterceptor.setFormat(true);
        return performanceInterceptor;
    }*/
}
